MODULE INMSOIL_ARRAYS


REAL, ALLOCATABLE :: T2(:,:),TS(:,:),QS(:,:),RHOA(:,:),SNOW(:,:)

REAL, ALLOCATABLE :: BSN(:,:),STOK(:,:),WINT(:,:),TINT(:,:),WINT20(:,:), &
& WINT50(:,:),WINT100(:,:),ELATENT(:,:),EFLUX(:,:)

REAL, ALLOCATABLE :: ERAD(:,:),PHEAT(:,:),T_LAYER2(:,:)

!REAL :: DZNORM, DZMIN, DEPTH, UPPERLAYER

REAL, ALLOCATABLE :: ST(:,:),PGR(:,:),TGROLD(:,:),QGROLD(:,:), &
& RADIAT(:,:),PRECIP(:,:),WSOLD(:,:),SNOLD(:,:)

REAL, ALLOCATABLE :: ZS(:,:),THSOIL(:,:),WHSOIL(:,:),BOLD(:,:), &
& RF1(:,:),RF2(:,:),SNMELT(:,:),SNMELT_OLD(:,:)

REAL, ALLOCATABLE :: HSNOW(:,:),HS(:,:),ES(:,:),TGRNEW(:,:),QGRNEW(:,:), &
& WSNEW(:,:),SNNEW(:,:),RUNOF1(:,:)

REAL, ALLOCATABLE :: ELATOLD(:,:),HFOLD(:,:),PRS(:,:),SHORTWAVE(:,:), &
& BW(:,:),BMELT(:,:),PRSOLD(:,:)

REAL, ALLOCATABLE :: AZ0P(:,:),VEG(:,:),ALBEDO_GR(:,:)

REAL, ALLOCATABLE :: WL(:,:,:) !ML

REAL, ALLOCATABLE :: RHOGR(:,:,:),WLM0(:,:,:),WLM7(:,:,:),BH(:,:,:),PSIMAX(:,:,:) !ML

REAL, ALLOCATABLE :: POR(:,:,:),FLWMAX(:,:,:),DLMAX(:,:,:) ! ML

REAL, ALLOCATABLE :: DZ(:,:,:) !MS

REAL, ALLOCATABLE :: EXTINCT(:,:,:) ! MS

REAL, ALLOCATABLE :: DENSNY(:,:),SN_INTRCP(:,:)

REAL, ALLOCATABLE :: CC(:,:,:) !ML

REAL, ALLOCATABLE :: AL(:,:,:),DLT(:,:,:),DVT(:,:,:),ALL(:,:,:),DL(:,:,:), &
& ALV(:,:,:),DV(:,:,:),Z(:,:,:),T(:,:,:) ! ML

REAL, ALLOCATABLE :: WV(:,:,:),WI(:,:,:),DENS(:,:,:) ! ML

REAL, ALLOCATABLE :: ROOTS(:,:,:),WSSL(:,:,:) ! ML

REAL, ALLOCATABLE :: WSSG(:,:,:) ! ML

REAL, ALLOCATABLE :: FFF(:,:),AMN(:,:),DVEG(:,:),RCO(:,:)

REAL, ALLOCATABLE :: HINT(:,:),SVEG(:,:),SBARE(:,:),ROOTSM(:,:)

REAL, ALLOCATABLE :: Q_METHANE(:,:,:),TGRANN(:,:,:) ! ML

REAL, ALLOCATABLE :: FPLANT(:,:),FEBUL(:,:),FDIFF(:,:),FTOT(:,:)

REAL, ALLOCATABLE :: PLANT_SUM(:,:),BULL_SUM(:,:),OXID_SUM(:,:), &
& RPROD_SUM(:,:),ANOX(:,:),WAT_TAB(:,:)

REAL, ALLOCATABLE :: WSMAX(:,:,:) ! ML

REAL, ALLOCATABLE :: WSL(:,:)

REAL, ALLOCATABLE :: HSNOLD(:,:)

REAL :: VKC,ANU,Z0MIN,CHC,P_M,P_T,Q_M,Q_T ! constants

REAL :: ALPHA_M,ALPHA_T,BETA_M,BETA_T,GAMMA_M,GAMMA_T  ! constants

REAL :: ZETA_ST,A_M,A_T,B_M,B_T ! constants

REAL, ALLOCATABLE :: T_SOIL_OUT(:,:), T_VEGET_OUT(:,:), T_BARE_SOIL_OUT(:,:), &
& T_WATER_OUT(:,:), T_SNOW_OUT(:,:)

REAL, ALLOCATABLE :: Q_SOIL_OUT(:,:)

REAL, ALLOCATABLE :: RADIAT_OUT(:,:), LHFLUX_OUT(:,:), SHFLUX_OUT(:,:), &
& CDM_OUT(:,:), GSOLO_OUT(:,:), SSBB_OUT(:,:), PRECIP_OUT(:,:),PRECIP_ACC(:,:)

REAL, ALLOCATABLE :: EMISSIVITY_OUT(:,:), RHOA_OUT(:,:)

REAL, ALLOCATABLE :: METHANE_OUT(:,:,:) ! ML

REAL, ALLOCATABLE :: TSG(:,:,:), WSG(:,:,:), VSG(:,:,:), WIG(:,:,:) ! ML

REAL, ALLOCATABLE :: BG(:,:)

REAL, ALLOCATABLE :: WSOIL(:,:),WSKIN(:,:),WSKINNEW(:,:),T0(:,:)

REAL, ALLOCATABLE :: CCQ(:,:),SWW(:,:),TL(:,:),LW(:,:)

REAL, ALLOCATABLE :: TSRFOL(:,:,:),TSRF(:,:,:)

REAL, ALLOCATABLE :: FBL(:) ! constants

REAL, ALLOCATABLE :: COND(:,:), PHASE(:,:)

REAL, ALLOCATABLE :: AN5(:,:)

real(8), allocatable :: dc6(:,:,:), litter_in(:,:,:), dnpp(:,:,:)
real(8), allocatable :: cpool_soil(:,:,:), npool_soil(:,:,:)

real(8), allocatable :: npp(:,:), cflux(:,:), c6(:,:) 
real(8), allocatable :: TACC(:,:,:), WLACC(:,:,:), STOKACC(:,:)

!real(8), allocatable :: fcveg(:,:,:)

INTEGER :: IYEAR,IMONTH,IDAY

!INTEGER :: KL, ML, MS, NT, NUM_SOIL, NUM_VEGET

INTEGER :: NSPIN


LOGICAL :: PHASETRANS

contains
SUBROUTINE INMSOIL_ARRAYS_ALLOC(i0,i1,j0,j1,ml,ms)

implicit none

include 'soil.inc'

! Input variables
integer, intent(in) :: i0, i1, j0, j1, ml, ms

allocate (T2(i0:i1,j0:j1),TS(i0:i1,j0:j1),QS(i0:i1,j0:j1), &
& RHOA(i0:i1,j0:j1),SNOW(i0:i1,j0:j1))

allocate (BSN(i0:i1,j0:j1),STOK(i0:i1,j0:j1),WINT(i0:i1,j0:j1), &
& TINT(i0:i1,j0:j1),WINT20(i0:i1,j0:j1),WINT50(i0:i1,j0:j1), &
& WINT100(i0:i1,j0:j1),ELATENT(i0:i1,j0:j1),EFLUX(i0:i1,j0:j1))
allocate(STOKACC(i0:i1,j0:j1))
allocate (ERAD(i0:i1,j0:j1),PHEAT(i0:i1,j0:j1),T_LAYER2(i0:i1,j0:j1))

allocate (ST(i0:i1,j0:j1),PGR(i0:i1,j0:j1),TGROLD(i0:i1,j0:j1), &
& QGROLD(i0:i1,j0:j1),RADIAT(i0:i1,j0:j1),PRECIP(i0:i1,j0:j1), &
& WSOLD(i0:i1,j0:j1),SNOLD(i0:i1,j0:j1))

allocate (ZS(i0:i1,j0:j1),THSOIL(i0:i1,j0:j1),WHSOIL(i0:i1,j0:j1), &
& BOLD(i0:i1,j0:j1),RF1(i0:i1,j0:j1),RF2(i0:i1,j0:j1), &
& SNMELT(i0:i1,j0:j1),SNMELT_OLD(i0:i1,j0:j1))

allocate (HSNOW(i0:i1,j0:j1),HS(i0:i1,j0:j1),ES(i0:i1,j0:j1), &
& TGRNEW(i0:i1,j0:j1),QGRNEW(i0:i1,j0:j1),WSNEW(i0:i1,j0:j1), &
& SNNEW(i0:i1,j0:j1),RUNOF1(i0:i1,j0:j1))

allocate (ELATOLD(i0:i1,j0:j1),HFOLD(i0:i1,j0:j1),PRS(i0:i1,j0:j1), &
& SHORTWAVE(i0:i1,j0:j1),BW(i0:i1,j0:j1),BMELT(i0:i1,j0:j1),PRSOLD(i0:i1,j0:j1))

allocate (AZ0P(i0:i1,j0:j1),VEG(i0:i1,j0:j1),ALBEDO_GR(i0:i1,j0:j1))

allocate (WL(ML,i0:i1,j0:j1)) !ML
allocate (WLACC(ML,i0:i1,j0:j1)) !ML


allocate (RHOGR(ML,i0:i1,j0:j1),WLM0(ML,i0:i1,j0:j1), &
& WLM7(ML,i0:i1,j0:j1),BH(ML,i0:i1,j0:j1),PSIMAX(ML,i0:i1,j0:j1)) !ML

allocate (POR(ML,i0:i1,j0:j1),FLWMAX(ML,i0:i1,j0:j1),DLMAX(ML,i0:i1,j0:j1)) ! ML

allocate (DZ(MS,i0:i1,j0:j1)) !MS

allocate (EXTINCT(MS,i0:i1,j0:j1)) ! MS

allocate (DENSNY(i0:i1,j0:j1),SN_INTRCP(i0:i1,j0:j1))

allocate (CC(ML,i0:i1,j0:j1)) !ML

allocate (AL(ML,i0:i1,j0:j1),DLT(ML,i0:i1,j0:j1),DVT(ML,i0:i1,j0:j1), &
& ALL(ML,i0:i1,j0:j1),DL(ML,i0:i1,j0:j1),ALV(ML,i0:i1,j0:j1), &
& DV(ML,i0:i1,j0:j1),Z(ML,i0:i1,j0:j1),T(ML,i0:i1,j0:j1)) ! ML
allocate (TACC(ML,i0:i1,j0:j1))

allocate (WV(ML,i0:i1,j0:j1),WI(ML,i0:i1,j0:j1),DENS(ML,i0:i1,j0:j1)) ! ML

allocate (ROOTS(ML,i0:i1,j0:j1),WSSL(ML,i0:i1,j0:j1)) ! ML

allocate (WSSG(ML,i0:i1,j0:j1)) ! ML

allocate (FFF(i0:i1,j0:j1),AMN(i0:i1,j0:j1),DVEG(i0:i1,j0:j1),RCO(i0:i1,j0:j1))

allocate (HINT(i0:i1,j0:j1),SVEG(i0:i1,j0:j1),SBARE(i0:i1,j0:j1),ROOTSM(i0:i1,j0:j1))

allocate (Q_METHANE(ML,i0:i1,j0:j1),TGRANN(ML,i0:i1,j0:j1)) ! ML

allocate (FPLANT(i0:i1,j0:j1),FEBUL(i0:i1,j0:j1),FDIFF(i0:i1,j0:j1),FTOT(i0:i1,j0:j1))

allocate (PLANT_SUM(i0:i1,j0:j1),BULL_SUM(i0:i1,j0:j1),OXID_SUM(i0:i1,j0:j1), &
& RPROD_SUM(i0:i1,j0:j1),ANOX(i0:i1,j0:j1),WAT_TAB(i0:i1,j0:j1))

allocate (WSMAX(ML,i0:i1,j0:j1)) ! ML

allocate (WSL(i0:i1,j0:j1))

allocate (HSNOLD(i0:i1,j0:j1))

allocate (T_SOIL_OUT(i0:i1,j0:j1), T_VEGET_OUT(i0:i1,j0:j1), &
& T_BARE_SOIL_OUT(i0:i1,j0:j1), T_WATER_OUT(i0:i1,j0:j1), T_SNOW_OUT(i0:i1,j0:j1))

allocate (Q_SOIL_OUT(i0:i1,j0:j1))

allocate (RADIAT_OUT(i0:i1,j0:j1), LHFLUX_OUT(i0:i1,j0:j1), &
& SHFLUX_OUT(i0:i1,j0:j1), CDM_OUT(i0:i1,j0:j1), &
& GSOLO_OUT(i0:i1,j0:j1), SSBB_OUT(i0:i1,j0:j1), &
& PRECIP_OUT(i0:i1,j0:j1), PRECIP_ACC(i0:i1,j0:j1))

allocate (EMISSIVITY_OUT(i0:i1,j0:j1), RHOA_OUT(i0:i1,j0:j1))

allocate (METHANE_OUT(ML,i0:i1,j0:j1)) ! ML

allocate (TSG(ML,i0:i1,j0:j1), WSG(ML,i0:i1,j0:j1), &
& VSG(ML,i0:i1,j0:j1), WIG(ML,i0:i1,j0:j1)) ! ML

allocate (BG(i0:i1,j0:j1))

allocate (WSOIL(i0:i1,j0:j1),WSKIN(i0:i1,j0:j1),WSKINNEW(i0:i1,j0:j1), &
& T0(i0:i1,j0:j1))

allocate (CCQ(i0:i1,j0:j1),SWW(i0:i1,j0:j1),TL(i0:i1,j0:j1),LW(i0:i1,j0:j1))

allocate (TSRFOL(4,i0:i1,j0:j1),TSRF(4,i0:i1,j0:j1))

allocate (FBL(20))

allocate (COND(i0:i1,j0:j1), PHASE(i0:i1,j0:j1))

allocate (AN5(i0:i1,j0:j1))

allocate (dc6(1:nv2,i0:i1,j0:j1), litter_in(1:nv2,i0:i1,j0:j1), dnpp(1:nv2,i0:i1,j0:j1))
allocate (cpool_soil(1:nsp,i0:i1,j0:j1), npool_soil(1:nsp,i0:i1,j0:j1))

allocate (npp(i0:i1,j0:j1), cflux(i0:i1,j0:j1), c6(i0:i1,j0:j1))

!allocate (fcveg(1:nv2,i0:i1,j0:j1))

END SUBROUTINE INMSOIL_ARRAYS_ALLOC
END MODULE INMSOIL_ARRAYS     
